package cn.ilovese.collect.serviceimpl.recommend.strategy;

import cn.ilovese.collect.datamapper.device.DeviceRequirementMapper;
import cn.ilovese.collect.datamapper.user.UserMapper;
import cn.ilovese.collect.po.task.Task;
import cn.ilovese.collect.service.recommend.strategy.RecommendStrategy;
import cn.ilovese.collect.util.RecommendUtil;
import cn.ilovese.collect.vo.task.TaskRecommendVO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class WorkerStaticMatchingTaskStrategy implements RecommendStrategy {
    private static WorkerStaticMatchingTaskStrategy instance;

    private List<Double> vector;

    @Resource
    DeviceRequirementMapper deviceRequirementMapper;

    @Override
    public List<TaskRecommendVO> recommendExecute(List<Task> allTask,UserMapper userMapper, DeviceRequirementMapper deviceRequirementMapper) {
        List<TaskRecommendVO> res=new ArrayList<>();
        for(int i=0;i<allTask.size();i++){
            res.add(new TaskRecommendVO(allTask.get(i),userMapper.selectByPrimaryKey(allTask.get(i).getUserId()).getUsername()));
        }
        for(int i=0;i<res.size();i++){
            res.get(i).setMatchity(
                    RecommendUtil.calculateMatchity(vector,
                            RecommendUtil.initTaskVector(allTask.get(i), deviceRequirementMapper.selectByTask(allTask.get(i).getId())))
            );
        }
        return res;
    }

    public void setVector(List<Double> vector) {
        this.vector = vector;
    }

    private WorkerStaticMatchingTaskStrategy(){
    }

    public static WorkerStaticMatchingTaskStrategy getInstance(){
        if(instance==null){
            instance=new WorkerStaticMatchingTaskStrategy();
        }
        return instance;
    }

}
